<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
     xmlns:h="http://java.sun.com/jsf/html"
     xmlns:f="http://java.sun.com/jsf/core"
     xmlns:ui="http://java.sun.com/jsf/facelets"
     xmlns:c="http://java.sun.com/jsp/jstl/core"
     xmlns:p="http://primefaces.org/ui"
     xmlns:o="http://omnifaces.org/ui"
     xmlns:jsf="http://xmlns.jcp.org/jsf">

<ui:fragment rendered="#{widgetWrapper.widgetView}">
    <p class="help-block">
        <h:outputFormat value="#{bundle['file.dataFilesTab.versions.widget.viewMoreInfo']}" escape="false">
            <f:param value="#{DatasetPage.dataset.globalIdString}"/>
            <f:param value="#{DatasetPage.dataset.displayName}"/>
            <f:param value="#{dataverseServiceBean.findRootDataverse().displayName}"/>
        </h:outputFormat>
    </p>
</ui:fragment>

<ui:fragment rendered="#{!widgetWrapper.widgetView}">
<!-- VERSIONS -->
   <script>
       //<![CDATA[
       // Version tab: Retrieve data after page load
        $(document).ready(function () {
            preload_message = "#{bundle['file.dataFilesTab.versions.preloadmessage']}";
            $('#datasetForm\\:tabView\\:versionsTable_data tr.ui-datatable-empty-message td').text(preload_message);
            postLoadVersionTabList();
       });
       //]]>
   </script>
   <!-- Retrieve version differences after the page load -->
   <p:remoteCommand  name="postLoadVersionTabList"
           process="@this"
           update=":#{p:resolveClientId('datasetForm:tabView:versionsTable', view)}"
           actionListener="#{DatasetPage.postLoadSetVersionTabList()}" />
    <div class="text-right margin-bottom">
        <ui:fragment rendered="#{DatasetPage.compareVersionsCount > 2}">
            <button type="button" class="btn btn-default" onclick="testCheckBoxes();">
                <span class="glyphicon glyphicon-transfer"/> #{bundle['file.dataFilesTab.versions.viewDiffBtn']}
            </button>
        </ui:fragment>
        <p:commandButton value="#{bundle['file.dataFilesTab.button.direct']}" id="compareVersions"
                         style="display:none"
                         update=":datasetForm"
                         oncomplete="PF('detailsBlocks').show();post_differences();"
                         actionListener="#{DatasetPage.compareVersionDifferences()}">
        </p:commandButton>
    </div>

    <p:dataTable id="versionsTable" value="#{DatasetPage.versionTabListForPostLoad}" var="versionTab" widgetVar="versionsTable"
                 rowIndexVar="rowNum" selection="#{DatasetPage.selectedVersions}" disabledSelection="#{versionTab.deaccessioned}" rowKey="#{versionTab}">
        <!-- start: checkbox column -->
        <p:column selectionMode="multiple" class="col-select-width text-center" rendered="#{DatasetPage.compareVersionsCount > 2}"/><!-- end: checkbox column -->

        <!-- start: version number column -->
        <p:column headerText="#{bundle['file.dataFilesTab.versions.headers.dataset']}" class="col-sm-1 text-center">
            <ui:fragment rendered="#{versionTab.released 
                                     or ((versionTab.deaccessioned or versionTab.draft) and permissionServiceBean.on(DatasetPage.dataset).has('ViewUnpublishedDataset'))}">
                <ui:fragment rendered="#{!(versionTab.released or versionTab.deaccessioned)}">
                    <a id="versionLink" href="/dataset.xhtml?persistentId=#{versionTab.dataset.globalIdString}&#38;version=#{versionTab.versionState}" class="ui-commandlink ui-widget">
                        <h:outputText rendered="#{!(versionTab.released or versionTab.deaccessioned)}" value="#{versionTab.versionState}" /></a>
                </ui:fragment>
                <ui:fragment rendered="#{(versionTab.released or versionTab.deaccessioned)}">
                    <a id="versionLink" href="/dataset.xhtml?persistentId=#{versionTab.dataset.globalIdString}&#38;version=#{versionTab.versionNumber}.#{versionTab.minorVersionNumber}" class="ui-commandlink ui-widget">
                        <h:outputText rendered="#{versionTab.released or versionTab.deaccessioned}" value="#{versionTab.versionNumber}.#{versionTab.minorVersionNumber}" /></a>
                </ui:fragment>
            </ui:fragment>
            <ui:fragment rendered="#{versionTab.deaccessioned and !permissionServiceBean.on(DatasetPage.dataset).has('ViewUnpublishedDataset')}">
                <h:outputText rendered="#{versionTab.released or versionTab.deaccessioned}" value="#{versionTab.versionNumber}.#{versionTab.minorVersionNumber}" />
            </ui:fragment>
        </p:column><!-- end: version number column -->
        <!-- start: description column -->
        <p:column headerText="#{bundle['file.dataFilesTab.versions.headers.summary']}">
            <ui:fragment rendered="#{versionTab.defaultVersionDifference != null}">
                <ui:fragment rendered="#{!empty(versionTab.defaultVersionDifference.summaryDataForNote)}">
                    <h:outputText styleClass="highlightBold" value="#{bundle['file.dataFilesTab.versions.citationMetadata']} " />
                    <ui:repeat value="#{versionTab.defaultVersionDifference.summaryDataForNote}" var="summaryNote">
                        <h:outputText value=" #{summaryNote[0].datasetFieldType.displayName} (" />
                        <h:outputText rendered="#{(summaryNote[1]) > 0 and summaryNote[0].datasetFieldType.allowMultiples}" value="#{summaryNote[1]} #{bundle['file.dataFilesTab.versions.added']}" />
                        <h:outputText rendered="#{(summaryNote[1]) > 0 and !(summaryNote[0].datasetFieldType.allowMultiples)}"  value="#{bundle['file.dataFilesTab.versions.added']}" />
                        <h:outputText rendered="#{(summaryNote[1]) > 0 and (summaryNote[2] + summaryNote[3]) > 0}" value=", " />
                        <h:outputText rendered="#{(summaryNote[2]) > 0 and summaryNote[0].datasetFieldType.allowMultiples}" value="#{summaryNote[2]} #{bundle['file.dataFilesTab.versions.removed']}" />
                        <h:outputText rendered="#{(summaryNote[2]) > 0 and !(summaryNote[0].datasetFieldType.allowMultiples)}" value="#{bundle['file.dataFilesTab.versions.removed']}" />
                        <h:outputText rendered="#{(summaryNote[2]) > 0 and (summaryNote[3]) > 0}" value=", " />
                        <h:outputText rendered="#{(summaryNote[3]) > 0 and summaryNote[0].datasetFieldType.allowMultiples}" value="#{summaryNote[3]} #{bundle['file.dataFilesTab.versions.changed']}" />
                        <h:outputText rendered="#{(summaryNote[3]) > 0 and !(summaryNote[0].datasetFieldType.allowMultiples)}" value="#{bundle['file.dataFilesTab.versions.changed']}" />
                        <h:outputText value="); " />
                    </ui:repeat>
                </ui:fragment>
                <ui:fragment rendered="#{!empty(versionTab.defaultVersionDifference.blockDataForNote)}">
                    <ui:repeat value="#{versionTab.defaultVersionDifference.blockDataForNote}" var="blockNote">
                        <h:outputText styleClass="highlightBold" rendered="#{blockNote[0].datasetFieldType.metadataBlock.displayName == 'Citation Metadata'}" value="#{bundle['file.dataFilesTab.versions.additionalCitationMetadata']} " />
                        <h:outputText styleClass="highlightBold" rendered="#{!(blockNote[0].datasetFieldType.metadataBlock.displayName == 'Citation Metadata')}" value=" #{blockNote[0].datasetFieldType.metadataBlock.displayName}: " />
                        <h:outputText value=" (" />
                        <h:outputText rendered="#{blockNote[1] > 0}" value="#{blockNote[1]} #{bundle['file.dataFilesTab.versions.added']}" />
                        <h:outputText rendered="#{(blockNote[1]) > 0 and (blockNote[2] + blockNote[3]) > 0}" value=", " />
                        <h:outputText rendered="#{(blockNote[2]) > 0}" value="#{blockNote[2]} #{bundle['file.dataFilesTab.versions.removed']}" />
                        <h:outputText rendered="#{(blockNote[2]) > 0 and (blockNote[3]) > 0}" value=", " />
                        <h:outputText rendered="#{(blockNote[3]) > 0}" value="#{blockNote[3]} #{bundle['file.dataFilesTab.versions.changed']}" />
                        <h:outputText value="); " />
                    </ui:repeat>
                </ui:fragment>
                <ui:fragment rendered="#{!empty(versionTab.defaultVersionDifference.fileNote)}">
                    <h:outputText styleClass="highlightBold" value="#{versionTab.defaultVersionDifference.fileNote}; " />
                </ui:fragment>
                <ui:fragment rendered="#{!empty(versionTab.defaultVersionDifference.changedTermsAccess)}">
                    <h:outputText styleClass="highlightBold" value="#{bundle['dataset.versionDifferences.termsOfUseAccessChanged']} " />
                </ui:fragment>
            </ui:fragment>
            <ui:fragment rendered="#{versionTab.defaultVersionDifference == null}">
                <ui:fragment rendered="#{versionTab.draft}">
                    #{bundle['file.dataFilesTab.versions.description.draft']}
                </ui:fragment>
                <ui:fragment rendered="#{versionTab.released and versionTab.priorVersionState == 'DEACCESSIONED'}">
                    #{bundle['file.dataFilesTab.versions.description.deaccessioned']}
                </ui:fragment>
                <ui:fragment rendered="#{versionTab.released and versionTab.priorVersionState == null}">
                    #{bundle['file.dataFilesTab.versions.description.firstPublished']}
                </ui:fragment>
                <ui:fragment rendered="#{versionTab.deaccessioned}">
                    #{bundle['file.dataFilesTab.versions.description.deaccessionedReason']} #{versionTab.versionNote} <ui:fragment rendered="#{!empty versionTab.archiveNote}">#{bundle['file.dataFilesTab.versions.description.beAccessedAt']} <a href="#{versionTab.archiveNote}" target="_blank">#{versionTab.archiveNote}</a></ui:fragment>
                </ui:fragment>
            </ui:fragment>
            <p:commandLink rendered="#{(!empty(versionTab.defaultVersionDifference)) and DatasetPage.versionTabListForPostLoad.size() > (rowNum + 1)}"
                           actionListener="#{DatasetPage.updateVersionDifferences(versionTab, null)}"
                           oncomplete="PF('detailsBlocks').show();post_differences();"
                           update=":datasetForm"
                           value="#{bundle['file.dataFilesTab.versions.viewDetails.btn']}"></p:commandLink>
        </p:column><!-- end: description column -->

        <!-- contributor column -->
        <p:column headerText="#{bundle['file.dataFilesTab.versions.headers.contributors']}" class="col-sm-3">
            <ui:fragment rendered="#{!empty(versionTab.contributorNames)}">
                <h:outputText value="#{versionTab.contributorNames}" />
            </ui:fragment>
        </p:column><!-- end: contributor column -->

        <!-- date column -->
        <p:column headerText="#{bundle['file.dataFilesTab.versions.headers.published']}" class="col-sm-2">
            <ui:fragment><!-- rendered="#{ !empty(versionTab) and !empty(versionTab.versionDate) }"-->
                <h:outputText id="versionDate" value="#{versionTab.versionDate}" />
            </ui:fragment>
        </p:column><!-- end: date column -->
    </p:dataTable>
<!-- / VERSIONS -->
</ui:fragment>
</ui:composition>
